<template>
  <el-row :gutter="22">
    <el-col :span="6">
      <div class="grid-content bg-purple">
        <Category @tree-node-click="treeNodeClick"></Category>
      </div>
    </el-col>

    <el-col :span="16">
      <div class="grid-content bg-purple">
        <div class="mod-config">
          <el-form
            :inline="true"
            :model="dataForm"
            @keyup.enter.native="getDataList()"
          >
            <el-form-item>
              <el-input
                v-model="dataForm.key"
                placeholder="参数名"
                clearable
              ></el-input>
            </el-form-item>
            <el-form-item>
              <el-button @click="getDataList()" type="primary" icon="el-icon-search">查询</el-button>
              <el-button
                v-if="isAuth('product:attr:save')"
                type="success"  icon="el-icon-plus"
                @click="addOrUpdateHandle()"
                >新增</el-button
              >
              <el-button type="info"  icon="el-icon-refresh" @click="resetList()" plain>重置</el-button>

              <el-button
                v-if="isAuth('product:attr:delete')"
                type="danger"
                @click="deleteHandle()"
                :disabled="dataListSelections.length <= 0"
                >批量删除</el-button
              >
            </el-form-item>
          </el-form>
          <el-table
            :data="dataList"
            border
            v-loading="dataListLoading"
            @selection-change="selectionChangeHandle"
            style="width: 100%"
          >
            <el-table-column
              type="selection"
              header-align="center"
              align="center"
              width="50"
            >
            </el-table-column>
            <el-table-column
              prop="attrId"
              header-align="center"
              align="center"
              label="ID"
            >
            </el-table-column>
            <el-table-column
              prop="attrName"
              header-align="center"
              align="center"
              label="属性名"
            >
            </el-table-column>

            <!-- <el-table-column
              prop="icon"
              header-align="center"
              align="center"
              label="属性图标"
            >
            </el-table-column> -->
            <el-table-column
              prop="valueSelect"
              header-align="center"
              align="center"
              label="可选值列表"
            >
              <template slot-scope="scope">
                <el-tooltip placement="top">
                  <div slot="content">
                    <span v-for="(i,index) in scope.row.valueSelect.split(',')" :key="index">{{i}}<br/></span>
                  </div>
                  <el-tag>{{scope.row.valueSelect.split(",")[0]+" "}}</el-tag>
                </el-tooltip>
              </template>
            </el-table-column>
            <el-table-column
              prop="attrType"
              header-align="center"
              align="center"
              label="属性类型"
            >
            </el-table-column>
            <el-table-column
              prop="catelogName"
              header-align="center"
              align="center"
              label="分类名称"
            >
            </el-table-column>
            <el-table-column
              prop="groupName"
              header-align="center"
              align="center"
              label="分组名称"
            >
            </el-table-column>
            <el-table-column
              prop="searchType"
              header-align="center"
              align="center"
              label="是否检索"
            >
              <template slot-scope="scope">
                <i class="el-icon-success" v-if="scope.row.searchType==1"></i>
                <i class="el-icon-error" v-else></i>
              </template>
            </el-table-column>
            <el-table-column
              prop="enable"
              header-align="center"
              align="center"
              label="启用状态"
            >
              <template slot-scope="scope">
                <i class="el-icon-success" v-if="scope.row.enable==1"></i>
                <i class="el-icon-error" v-else></i>
              </template>
            </el-table-column>
            <el-table-column
              prop="showDesc"
              header-align="center"
              align="center"
              label="是否快速展示"
            >
              <template slot-scope="scope">
                <i class="el-icon-success" v-if="scope.row.showDesc==1"></i>
                <i class="el-icon-error" v-else></i>
              </template>
            </el-table-column>
            <el-table-column
              fixed="right"
              header-align="center"
              align="center"
              width="150"
              label="操作"
            >
              <template slot-scope="scope">
                <el-button
                  type="text"
                  size="small"
                  @click="addOrUpdateHandle(scope.row.attrId)"
                  >修改</el-button
                >
                <el-button
                  type="text"
                  size="small"
                  @click="deleteHandle(scope.row.attrId)"
                  >删除</el-button
                >
              </template>
            </el-table-column>
          </el-table>
          <el-pagination
            @size-change="sizeChangeHandle"
            @current-change="currentChangeHandle"
            :current-page="pageIndex"
            :page-sizes="[10, 20, 50, 100]"
            :page-size="pageSize"
            :total="totalPage"
            layout="total, sizes, prev, pager, next, jumper"
          >
          </el-pagination>
          <!-- 弹窗, 新增 / 修改 -->
          <add-or-update v-if="addOrUpdateVisible" :type="attrType" ref="addOrUpdate" @refreshDataList="getDataList" ></add-or-update>
        </div>
      </div>
    </el-col>
  </el-row>
</template>

<script>
import AddOrUpdate from "./attr-add-or-update";
import Category from '../common/category-module'     //导入组件后,新命名组件名
export default {
  data() {
    return {
      dataForm: {
        key: "",
      },
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false,
    };
  },
  components: {
    AddOrUpdate,Category
  },
  props:{
    attrType:{
      type: Number,
      default: 1
    }
  },
  activated() {
    this.getDataList();
  },
  methods: {
    // 获取数据列表
    getDataList() {
      //console.log("attrtype: ", this.attrType);
      
      this.dataListLoading = true;
      this.$http({
        url: this.$http.adornUrl("/product/attr/list"),
        method: "get",
        params: this.$http.adornParams({
          page: this.pageIndex,
          limit: this.pageSize,
          key: this.dataForm.key,
          catelogId: this.dataForm.catelogId,
          attrType: this.attrType
        }),
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.dataList = data.page.list;
          this.totalPage = data.page.totalCount;
        } else {
          this.dataList = [];
          this.totalPage = 0;
        }
        this.dataListLoading = false;
      });
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.getDataList();
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val;
      this.getDataList();
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val;
    },
    // 新增 / 修改
    addOrUpdateHandle(id) {
      this.addOrUpdateVisible = true;
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id);
      });
    },
    // 重置
    resetList() {
      this.dataForm = this.$options.data().dataForm;
      this.getDataList();
    },
    //感知树节点被点击
    treeNodeClick(data,node){
      //console.log("树节点被点击:",data , node);
      
      //只处理3级分类的
      if(node.level != 3){
        return;
      }

      this.dataForm.catelogId = data.catId;
      this.getDataList();
    },
    // 删除
    deleteHandle(id) {
      var ids = id
        ? [id]
        : this.dataListSelections.map((item) => {
            return item.attrId;
          });
      this.$confirm(
        `确定对[id=${ids.join(",")}]进行[${id ? "删除" : "批量删除"}]操作?`,
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).then(() => {
        this.$http({
          url: this.$http.adornUrl("/product/attr/delete"),
          method: "post",
          data: this.$http.adornData(ids, false),
        }).then(({ data }) => {
          if (data && data.code === 0) {
            this.$message({
              message: "操作成功",
              type: "success",
              duration: 1500,
              onClose: () => {
                this.getDataList();
              },
            });
          } else {
            this.$message.error(data.msg);
          }
        });
      });
    },
  },
};
</script>
